﻿package lobby
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import lobby.events.LobbyEvent;
	import lobby.GlobalVariables;
	import lobby.exchangeddata.HashTable;
	import lobby.events.LobbyEvent;
	
	/**
	 * ...
	 * @author David
	 */
	public class LeftMenuPanel extends MovieClip
	{
		public var title_mc:MovieClip;
		public var casinoGroup_mc:MovieClip;
		public var gameGroup_mc:MovieClip;
		public var item_arr:Array = new Array();
		public var locateDefx:Number = 0;
		public var locateDefy:Number = -187;
		public var title_arr:Array = new Array("item1_mc", "item2_mc", "item3_mc", "item4_mc", "item5_mc");
		public var gameGroup_arr:Array = new Array("all_mc", "Ba_mc", "Bi_mc", "Ft_mc", "Dt_mc", "Dc_mc", "Ro_mc", "Sd_mc");
		public var arrange_arr:Array = new Array();
		public var listCasino_arr:Array = new Array();
		public var listGameCasino_arr:Array = new Array();
		public var listGame_arr:Array = new Array();
		public var countItem:Number = 6;
		private var _casinoID:String = "";
		private var countClick = 0;
		public function LeftMenuPanel()
		{
			this.tabChildren = false;
			item_arr = new Array();
			arrange_arr = new Array();
			listCasino_arr = new Array();
			listGameCasino_arr = new Array();
			
			title_mc.over_mc.visible = false;
			casinoGroup_mc.visible = false;
			gameGroup_mc.over_mc.visible = false;
			casinoGroup_mc.over_mc.visible = false;
			gameGroup_mc.bottom_mc.buttonMode = true;
			gameGroup_mc.top_mc.buttonMode = true;
			gameGroup_mc.bottom_mc.addEventListener(MouseEvent.ROLL_OVER, slideOverHandler);
			gameGroup_mc.top_mc.addEventListener(MouseEvent.ROLL_OVER, slideOverHandler);
			gameGroup_mc.bottom_mc.addEventListener(MouseEvent.ROLL_OUT, slideOutHandler);
			gameGroup_mc.top_mc.addEventListener(MouseEvent.ROLL_OUT, slideOutHandler);
			gameGroup_mc.bottom_mc.addEventListener(MouseEvent.CLICK, bottomBtnHandler);
			gameGroup_mc.top_mc.addEventListener(MouseEvent.CLICK, topBtnHandler);
		}
		
		public function initMenuLeft(listCasino:Array)
		{
			trace("vo -------------- " + listCasino.length);
			item_arr = new Array();
			arrange_arr = new Array();
			listCasino_arr = new Array();
			listGameCasino_arr = new Array();
			
			listCasino_arr = sortCasino(listCasino);
			separateCasino(listCasino_arr);
		}
		
		public function sortCasino(listCasino_arr:Array):Array {
			var _arr:Array = new Array();
			for (var i = 0; i < listCasino_arr.length; i++)
			{
				switch (listCasino_arr[i])
				{
					case "12": 
						_arr.push(12);
						break;
					case "0": 
						_arr.push(0);
						break;
					case "1": 
						_arr.push(1);
						break;
					case "2": 
						_arr.push(2);
						break;
					
				}
			}
			return _arr;
		}
		
		public function initMenuLocal()
		{
			title_mc.buttonMode = true;
			casinoGroup_mc.buttonMode = true;
			//add Event
			title_mc.addEventListener(MouseEvent.ROLL_OVER, titleOverHandler);
			title_mc.addEventListener(MouseEvent.ROLL_OUT, titleOutHandler);
			title_mc.addEventListener(MouseEvent.CLICK, titleClickHandler);
			//casinoGroup_mc.bg_mc.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			//casinoGroup_mc.bg_mc.addEventListener(MouseEvent.ROLL_OUT, outHandler);
			for (var i = 0; i < item_arr.length; i++)
			{
				casinoGroup_mc[item_arr[i]].addEventListener(MouseEvent.ROLL_OVER, casinoOverHandler);
				casinoGroup_mc[item_arr[i]].addEventListener(MouseEvent.ROLL_OUT, casinoOutHandler);
				casinoGroup_mc[item_arr[i]].addEventListener(MouseEvent.CLICK, casinoClickHandler);
			}
			// Hide/show item
			//default: HL
			showItemTitle(item_arr[0]);
			showItemCasino(item_arr[0]);
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.GET_LIST_GAME, true);
			event.casinoID = null;
			_casinoID = null;
			dispatchEvent(event);
			
			
			//default : GG
			/*showItemTitle("item3_mc");
			showItemCasino("item3_mc");
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.GET_LIST_GAME, true);
			event.casinoID = "2";
			_casinoID = "2";
			dispatchEvent(event);*/
		}
		
		public function separateCasino(_arr:Array)
		{
			item_arr.push("item1_mc");
			trace(_arr);
			_arr.sort(Array.DESCENDING|Array.NUMERIC);
			trace(_arr);
			for (var i = 0; i < _arr.length; i++)
			{
				trace("_arr : " + typeof(_arr[i]) + _arr[i]);
				switch (_arr[i])
				{
					case 12: 
						item_arr.push("item2_mc");
						break;
					case 0: 
						item_arr.push("item4_mc");
						break;
					case 1: 
						item_arr.push("item5_mc");
						break;
					case 2: 
						item_arr.push("item3_mc");
						break;
					
				}
				if (i == (_arr.length - 1))
				{
					initMenuLocal();
				}
			}
		}
		
		public function getCasinoID(name):String
		{
			var returnValue:String = "";
			switch (name)
			{
				case "item1_mc": 
					returnValue = null;
					break;
				case "item2_mc": 
					returnValue = "12"
					break;
				case "item3_mc": 
					returnValue = "2";
					break;
				case "item4_mc": 
					returnValue = "0"
					break;
				case "item5_mc": 
					returnValue = "1"
					break;
			}
			return returnValue;
		}
		
		/*public function overHandler(ev:MouseEvent):void
		{
			casinoGroup_mc.visible = true;
			title_mc.over_mc.visible = true;
		}
		
		public function outHandler(ev:MouseEvent):void
		{
			casinoGroup_mc.visible = false;
			title_mc.over_mc.visible = false;
		}*/
		
		public function titleOverHandler(ev:MouseEvent):void
		{
			//casinoGroup_mc.visible = true;
			title_mc.over_mc.visible = true;
		}
		
		public function titleOutHandler(ev:MouseEvent):void
		{
			//casinoGroup_mc.visible = false;
			title_mc.over_mc.visible = false;
		}
		
		public function titleClickHandler(ev:MouseEvent):void 
		{
			if (countClick == 0) {
				updateTextCasinoGroup();
				casinoGroup_mc.visible = true;
				countClick = 1;
			}else {
				casinoGroup_mc.visible = false;
				countClick = 0;
			}
		}
		private function updateTextCasinoGroup():void {
			var lang = "";
			if (GlobalVariables.lang == "G" || GlobalVariables.lang == "B") {
				lang = "G";
			}else {
				lang = "E";
			}
			for (var i = 0; i < item_arr.length; i++) {
				casinoGroup_mc[item_arr[i]].gotoAndStop(lang);
			}
			
		}
		public function casinoOverHandler(ev:MouseEvent):void
		{
			//title_mc.over_mc.visible = true;
			casinoGroup_mc.visible = true;
			casinoGroup_mc.over_mc.visible = true;
			casinoGroup_mc.over_mc.x = ev.currentTarget.x;
			casinoGroup_mc.over_mc.y = ev.currentTarget.y;
		}
		
		public function casinoOutHandler(ev:MouseEvent):void
		{
			//title_mc.over_mc.visible = false;
			//casinoGroup_mc.visible = false;
			casinoGroup_mc.over_mc.visible = false;
		}
		
		public function casinoClickHandler(ev:MouseEvent):void
		{
			countClick = 0;
			casinoGroup_mc.visible = false;
			//trace(ev.currentTarget.name);
			showItemTitle(ev.currentTarget.name);
			showItemCasino(ev.currentTarget.name);
			
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.GET_LIST_GAME, true);
			event.casinoID = getCasinoID(ev.currentTarget.name);
			_casinoID = event.casinoID
			dispatchEvent(event);
		}
		
		public function getListGameCasino(_arr:Array)
		{
			_arr.sortOn("getGameTypeIndex", Array.NUMERIC);
			for (var i = 0; i < _arr.length; i++) {
				trace("_arr : "+_arr[i].getGameType)
			}
			
			this.listGameCasino_arr = _arr;
			showListGame(listGameCasino_arr);
		}
		
		public function showItemTitle(item:String)
		{
			
			trace("item : " + item + " : " + item_arr.length);
			title_mc.item0_mc.visible = false;
			for (var j = 0; j < title_arr.length; j++) {
				title_mc[title_arr[j]].visible = false;
			}
			
			for (var i = 0; i < item_arr.length; i++)
			{
				if (item_arr[i] == item)				
				{
					//trace("ke : " + title_mc[item_arr[i]].name);
					title_mc[item_arr[i]].visible = true;
					var lang:String = "";
					if (GlobalVariables.lang == "G" || GlobalVariables.lang == "B") {
						lang = "G";
					}else {
						lang = "E";
					}
					if(i == 0){
						title_mc[item_arr[i]].gotoAndStop(lang);
						title_mc.item0_mc.visible = true;
						title_mc.item0_mc.gotoAndStop(lang);
					}
				}
			}
		}
		
		public function showItemCasino(item:String):void
		{
			arrange_arr = new Array();
			for (var j = 0; j < title_arr.length; j++) {
				casinoGroup_mc[title_arr[j]].visible = false;
			}
			for (var i = 0; i < item_arr.length; i++)
			{
				if (item_arr[i] != item)
				{
					casinoGroup_mc[item_arr[i]].visible = true;
					arrange_arr.push(item_arr[i]);
				}
				else
				{
					casinoGroup_mc[item_arr[i]].visible = false;
				}
			}
			//arrange_arr.sort(2);
			arrangeItem();
		}
		
		public function arrangeItem()
		{
			
			for (var i = 0; i < arrange_arr.length; i++)
			{
				trace("arrange_arr : " + arrange_arr[i]);
				casinoGroup_mc[arrange_arr[i]].x = locateDefx;
				casinoGroup_mc[arrange_arr[i]].y = locateDefy + (casinoGroup_mc[arrange_arr[i]].height + 10) * i;
			}
		}
		
		public function showListGame(listGame:Array)
		{
			//"Ba_mc","Bi_mc","Ft_mc","Dt_mc","Dc_mc","Ro_mc","Sd_mc";
			countItem = Math.min(6, listGame.length);
			mixListGameAndAll(listGame);
			for (var i = 0; i < gameGroup_arr.length; i++)
			{
				gameGroup_mc[gameGroup_arr[i]].visible = false;
			}
			var lang:String = "";
			if (GlobalVariables.lang == "G" || GlobalVariables.lang == "B") {
				lang = "G";
			}else {
				lang = "E";
			}
			gameGroup_mc[gameGroup_arr[0]].gotoAndStop(lang);
			gameGroup_mc[gameGroup_arr[0]].y = 0;
			gameGroup_mc[gameGroup_arr[0]].visible = true;
			gameGroup_mc[gameGroup_arr[0]].x = 0;				
			gameGroup_mc[gameGroup_arr[0]].buttonMode = true;
			gameGroup_mc[gameGroup_arr[0]].addEventListener(MouseEvent.ROLL_OVER, gameOverHandler);
			gameGroup_mc[gameGroup_arr[0]].addEventListener(MouseEvent.ROLL_OUT, gameOutHandler);
			gameGroup_mc[gameGroup_arr[0]].addEventListener(MouseEvent.CLICK, gameClickHandler);
			for (var j = 0; j < listGame.length; j++)
			{
				gameGroup_mc[listGame[j].getGameType + "_mc"].visible = true;
				gameGroup_mc[listGame[j].getGameType + "_mc"].gotoAndStop(lang);
				gameGroup_mc[listGame[j].getGameType + "_mc"].x = 0;
				gameGroup_mc[listGame[j].getGameType + "_mc"].y = gameGroup_mc[listGame[j].getGameType + "_mc"].height * (j+1);
				gameGroup_mc[listGame[j].getGameType + "_mc"].buttonMode = true;
				gameGroup_mc[listGame[j].getGameType + "_mc"].addEventListener(MouseEvent.ROLL_OVER, gameOverHandler);
				gameGroup_mc[listGame[j].getGameType + "_mc"].addEventListener(MouseEvent.ROLL_OUT, gameOutHandler);
				gameGroup_mc[listGame[j].getGameType + "_mc"].addEventListener(MouseEvent.CLICK, gameClickHandler);
			}
		}
		
		public function mixListGameAndAll(listGame:Array):void
		{
			listGame_arr = new Array();
			//listGame_arr = listGame;
			listGame_arr.push("all_mc");
			for (var j = 0; j < listGame.length; j++)
			{
				listGame_arr.push(listGame[j].getGameType + "_mc");
			}
		}
		public function gameOverHandler(ev:MouseEvent):void
		{
			gameGroup_mc.over_mc.visible = true;
			gameGroup_mc.over_mc.x = ev.currentTarget.x;
			gameGroup_mc.over_mc.y = ev.currentTarget.y;
		}
		
		public function gameOutHandler(ev:MouseEvent):void
		{
			gameGroup_mc.over_mc.visible = false;
		}
		
		public function gameClickHandler(ev:MouseEvent):void
		{
			var _str:String = ev.currentTarget.name;
			_str = _str.substring(0, _str.indexOf("_"));
			//khaii
			trace("game click : " + _str);
			
			if (_str == "all") {
				var eventAll:LobbyEvent = new LobbyEvent(LobbyEvent.GET_LIST_GAME, true);
				eventAll.casinoID = _casinoID;
				dispatchEvent(eventAll);
			}else {			
				var event:LobbyEvent = new LobbyEvent(LobbyEvent.GET_DETAIL_GAME, true);
				event.gameType = _str;
				event.casinoID = _casinoID;
				trace("event.gameType : " + event.gameType + " event.casinoID : " + event.casinoID);
				dispatchEvent(event);
			}
		}		
		
		public function bottomBtnHandler(ev:MouseEvent):void
		{
			if (listGame_arr.length > 6)
			{
				//trace(countItem + " : " + (listGame_arr.length - 1));
				if (countItem < listGame_arr.length)
				{
					for (var i = 0; i < listGame_arr.length; i++)
					{
						gameGroup_mc[listGame_arr[i]].y -= 84;
					}
					countItem++;
				}
			}
		}
		
		public function topBtnHandler(ev:MouseEvent):void
		{
			if (listGame_arr.length > 6)
			{
				//trace(countItem + " : " + (listGame_arr.length - 1));
				if ((countItem - 6) > 0)
				{
					for (var i = 0; i < listGame_arr.length; i++)
					{
						gameGroup_mc[listGame_arr[i]].y += 84;
					}
					countItem--;
				}
			}
		}
		
		public function slideOverHandler(ev:MouseEvent):void
		{
			ev.currentTarget.gotoAndStop("over");
		}
		
		public function slideOutHandler(ev:MouseEvent):void
		{
			ev.currentTarget.gotoAndStop("out");
		}
		
		public function changeTextLang():void
		{			
			trace("vo neeeeeeeee");
			var lang:String = "";
			if (GlobalVariables.lang == "G" || GlobalVariables.lang == "B") {
				lang = "G";
			}else {
				lang = "E";
			}			
			gameGroup_mc[gameGroup_arr[0]].gotoAndStop(lang);
			
			for (var j = 0; j < listGameCasino_arr.length; j++)
			{
				gameGroup_mc[listGameCasino_arr[j].getGameType + "_mc"].gotoAndStop(lang);
			}
			title_mc["item1_mc"].gotoAndStop(lang);
			title_mc.item0_mc.gotoAndStop(lang);
		}
	}
}

